package com.qingfeng;

import com.qingfeng.client.LoginHandlerInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.*;


@Slf4j
//@MapperScan({"com.portjs.base.dao","com.portjs.jintan.**.dao"})
@MapperScan({"com.qingfeng.**.dao"})
//如果service实现类中加入事务注解，需要此处添加该注解进行开启事务管理
@EnableTransactionManagement
@EnableScheduling
@SpringBootApplication
public class ClientApplication {


    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
        log.info("===================启动完成==========");
    }


    /**
     * 跨域两件套之一
     * @return
     */
    @Bean
    public CookieSerializer httpSessionIdResolver(){
        DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
        cookieSerializer.setCookieName("JSESSIONID");
        cookieSerializer.setUseHttpOnlyCookie(false);
        cookieSerializer.setSameSite(null);

        return cookieSerializer;
    }

    /**
     * 在拦截器中注入service
     * 2。关键，将拦截器作为bean写入配置中
     * @return
     */
    @Bean
    public LoginHandlerInterceptor myInterceptor(){
        return new LoginHandlerInterceptor();
    }

    @Bean //将组件注册在容器中
    public WebMvcConfigurer webMvcConfigurerAdapter(){

        return new WebMvcConfigurer(){

            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                InterceptorRegistration addInterceptor = registry.addInterceptor(myInterceptor());
                // 排除配置
                addInterceptor.excludePathPatterns("/");
                addInterceptor.excludePathPatterns("/static/**");//排除静态资源
                addInterceptor.excludePathPatterns("/app/buyer/login");
                addInterceptor.excludePathPatterns("/codeLogin");
                addInterceptor.excludePathPatterns("/wxpay/notify");//支付成功回调
                addInterceptor.excludePathPatterns("/#/**");
//                swagger文档放开
                addInterceptor.excludePathPatterns("/doc.html","/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
                // 拦截配置
                addInterceptor.addPathPatterns("/**");
            }
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");//
                //swagger
                registry.addResourceHandler("swagger-ui.html")
                        .addResourceLocations("classpath:/META-INF/resources/");
                registry.addResourceHandler("/webjars/**")
                        .addResourceLocations("classpath:/META-INF/resources/webjars/");
                registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
            }

            /**
             * 跨域两件套之二
             * @param registry
             */
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("*")
                        .allowedMethods("*")
                        .allowedHeaders("*")
                        .allowCredentials(true)
                        .maxAge(3600);

            }
        };
    }
}
